package com.bw.usercenter.ui.mainview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.View.INVISIBLE
import android.view.View.VISIBLE
import android.widget.TextView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bw.mvpcore.view.BaseMVPActivity
import com.bw.usercenter.R
import com.bw.usercenter.adapter.RecyclerviewAdapte
import com.bw.usercenter.adapter.RecyclerviewAdapter
import com.bw.usercenter.contract.UserCenterContract
import com.bw.usercenter.model.protocol.resp.HistoryBean
import com.bw.usercenter.model.protocol.resp.RespRecommendEntity
import com.bw.usercenter.model.protocol.resp.RespSearchEntity
import com.bw.usercenter.model.protocol.resp.RespSearchitemIDEntity
import com.bw.usercenter.presenter.UserCenterPresenterImpl
import kotlinx.android.synthetic.main.activity_find.*
import org.jetbrains.anko.toast

class FindActivity : BaseMVPActivity<UserCenterPresenterImpl>(), UserCenterContract.UserCenterView ,View.OnClickListener{
    //Recyclerview适配器
    var recyclerviewAdapter:RecyclerviewAdapter?=null
    //快速适配器
    var recyclerviewAdapte: RecyclerviewAdapte?=null

    var data :List<RespSearchEntity.Data>?=null

    var listhistory:List<HistoryBean> = arrayListOf()
    override fun getLayoutId(): Int {
        return R.layout.activity_find
    }
    override fun initEvent() {


        find_recycler.layoutManager = StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL)
//        find_recycler.visibility = INVISIBLE
        find_recycler.visibility = VISIBLE
        find_findd.setOnClickListener {
            var title = find_findd.text.trim().toString()
            if (title.length!=0){

                var itemCount = recyclerviewAdapter?.itemCount
                mPresenter.getSerch(title)
                val historyBean:HistoryBean = HistoryBean(title,R.drawable.delete_img)

                toast("$itemCount--${find_findd.text.trim().toString()}")

            }
        }


    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun ClearStatBar() {

    }
    override fun releaseResource() {

    }

    override fun createPresenter(): UserCenterPresenterImpl {
        return UserCenterPresenterImpl(this@FindActivity)
    }

    override fun <T> registerSuccess(data: T) {

    }

    override fun registerSuccess(data: RespRecommendEntity) {

    }

    override fun registerFaild(throwable: Throwable) {

    }

    override fun registerSrech(respSearchEntity: RespSearchEntity) {
        recyclerviewAdapter?.notifyDataSetChanged()
        data = respSearchEntity.data

        recyclerviewAdapter = RecyclerviewAdapter(this@FindActivity,data!!)
        find_recycler.adapter = recyclerviewAdapter

        recyclerviewAdapter?.setOnItemClickListener(object :RecyclerviewAdapter.OnItemClickListener{

            override fun onItemClick(v: View, int: Int) {
                var itemId = data?.get(int)?.ItemId
                var itemUrl = data?.get(int)?.ItemUrl
                var bundle:Bundle = Bundle()
                bundle.putString("itemid",itemId.toString())
                bundle.putString("itemUrl",itemUrl)
                jumpActivity(DetailsViewActivity::class.java,bundle)
            }

        })

        recyclerviewAdapte = RecyclerviewAdapte(data as  MutableList<RespSearchEntity.Data>?)

        //find_recycler.adapter =recyclerviewAdapte
//        recyclerviewAdapte.notifyDataSetChanged()

    }

    override fun getSearchitem(respSearchitemIDEntity: RespSearchitemIDEntity) {

    }

    override fun onClick(v: View?) {

    }
}